<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/23 0023
 * Time: 13:50
 */

namespace App\Http\Controllers\Admin\Auth;
use App\Http\Controllers\Admin\Controller;
use App\Models\AdminUser;
use App\Models\SystemConfig;
use Illuminate\Http\Request;
use Mews\Captcha\Facades\Captcha;
use Validator, DB;

class LoginController extends Controller
{
    /**
     * 待定配置
     * URL POST:/admin/public/conf
     * @return mixed
     */
    public function conf()
    {
        $system = SystemConfig::where('group_code', 'like', '%,admin,%')->pluck('val','code')->toArray();
        $data = [
            'domain_name' => (((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://') . env('DOMAIN_NAMES', $_SERVER['SERVER_NAME']),
        ];
        $data = array_merge($data,$system);
        return $this->success('查询成功',$data);
    }

    /**
     * 账号密码登录
     * URL POST:/admin/login
     * @param Request $request
     * @param username 用户名
     * @param password 密码
     * @return mixed
     */
    public function login(Request $request)
    {
        //接受参数
        $params = $request->only(['username','password']);
        $validator = Validator::make($params, [
            'username'   => ['required', 'max:11'],
            'password'  => ['required'],
        ], [
            'username.required'   => '请输入用户名',
            'password.required' => '请输入密码',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $username = $params['username'];
        $password = $params['password'];
        //判断账号是否存在
        $userOne = AdminUser::where('username', $username)->whereNull('deleted_at')->first();
        if (!$userOne) {
            return $this->error('该账户未不存在，联系管理员');
        }

        if ($userOne->status == 2) {
            return $this->error('您的账号被冻结，请联系管理员');
        }

        //判断是否可以生成token 是则登录成功 否则登录失败
        $userInfo = ['username' => $username, 'password' => $password];

        $token = $this->guard()->attempt($userInfo);
        if ( !$token) {
            return $this->error('账号或密码错误，请重试');
        }
        // return $this->success('登录成功',['token' => 'bearer '.$token]);
        return $this->success( '登录成功',compact('token'));
    }

    /**
     * 退出登录
     * URL GET:/admin/logout
     * @return array
     */
    public function logout()
    {
        try {
            // 获取当前用户
//            $user_id = $this->guard()->user()->id;

            // 查询最后一条登录记录
//            $login_log = LoginLog::where('type',2)->where('user_id',$user_id)->orderBy('time','desc')->first();
//
//            // 获取登录时长
//            $login_time = strtotime($login_log->time);
//
//            $login_length = time() - $login_time;

            // 修改登录日志
//            LoginLog::where('id',$login_log->id)->update([
//                'out_time'      => Carbon::now(),
//                'length_time'   => $login_length
//            ]);

            $this->guard()->logout();
        } catch (\Exception $e) {

            return $this->error('退出失败，请重试');
        }

        return $this->success('退出成功');
    }


}